package com.android.takeaway.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.math.BigDecimal;
import java.util.List;


import com.android.takeaway.R;
import com.android.takeaway.bean.Food;

/**
 * 购物车适配器
 * 连接数据和 UI 控件，将数据展示在用户界面上
 */
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder> {
    private List<Food> foodBeanList;
    private Context context;
    //声明接口对象
    private OnItemChangedListener onItemChangedListener;

    public void setOnItemChangedListener(OnItemChangedListener onItemChangedListener) {
        this.onItemChangedListener = onItemChangedListener;
    }
    //构造方法
    public CartAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Food> foodBeanList) {
        this.foodBeanList = foodBeanList;
        notifyDataSetChanged(); //刷新界面
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载购物车列表
        View view = LayoutInflater.from(context).inflate(R.layout.shopping_cart_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }
    //控件赋值
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Food foodBean = foodBeanList.get(position);
        holder.tv_foodName.setText(foodBean.getFoodName());
        holder.tv_foodCost.setText("￥ " + foodBean.getPrice().multiply(BigDecimal.valueOf(foodBean.getCount())));
        holder.tv_count.setText(foodBean.getCount() + "");
        holder.iv_minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用接口对象的方法
                onItemChangedListener.onItemMinus(foodBean);
            }
        });
        holder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemChangedListener.onItemAdd(foodBean);
            }
        });
    }
    @Override
    public int getItemCount() {
        //判断list里面是否为空，如果是空则返回0否则返回本身长度
        return foodBeanList == null ? 0 : foodBeanList.size();
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_foodName, tv_foodCost, tv_count;
        ImageView iv_minus, iv_add;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_foodName = itemView.findViewById(R.id.tv_foodName);
            tv_foodCost = itemView.findViewById(R.id.tv_foodCost);
            tv_count = itemView.findViewById(R.id.tv_count);
            iv_minus = itemView.findViewById(R.id.iv_minus);
            iv_add = itemView.findViewById(R.id.iv_add);
        }
    }
    public interface OnItemChangedListener {
        void onItemAdd(Food foodBean);
        void onItemMinus(Food fooodBean);
    }
}
